const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    page:1,
    pageSize:10,
    isEnd:false,
    adress:[],
    adressitem:'',
    id:'',
    prevpage:'',
    isloading:true
  },
  onLoad: function (options) {
    this.setData({
      id:options.id || '',
      prevpage:options.page
    })
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  onShow: function () {
    this.setData({
      page:1,
      pageSize:10,
      isEnd:false,
      adress:[],
    })
    this.addressList()
  },
  addressList(){
    var data={
      page:this.data.page,
      pageSize:this.data.pageSize
    }
    Controller.addressList(data).then(res=>{
      if(res.data.baseData && res.data.baseData.list.length>0){
        for(let item of res.data.baseData.list){
          item.select=false
          if(item.id==this.data.id){
            item.select=true
          }
        }
        this.setData({
          adress:this.data.adress.concat(res.data.baseData.list),
        })
        
        if(res.data.baseData.lastPage){
          this.setData({
            isEnd:res.data.baseData.lastPage
          })
        }
      }else{
        this.setData({
          isEnd:true
        })
      }
      this.setData({
        isloading:false
      })
    })
  },
  onReachBottom(){
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.addressList()
    }
    
  },
  default(e,id){
    var id='';
    if(e){
      id=e.currentTarget.dataset.id;
    }else{
      id=id
    }
    var lists=this.data.adress;
    Controller.setDefaultAddress({addressId:id}).then(res=>{
      for(let item of lists){
        item.isDefault=0
        if(item.id==id){
          item.isDefault=1
        }
      }
      this.setData({
        adress:lists
      })
    })
  },
  delete(e){
    var id=e.currentTarget.dataset.id;
    var lists=this.data.adress;
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否确定删除?',
      success(res) {
        if (res.confirm) {
          Controller.deleteAddress({addressId:id}).then(res=>{
            wx.showToast({
              title: "删除成功",
              icon: 'none',
              duration: 1000
            });
            that.setData({
              page:1,
              pageSize:10,
              isEnd:false,
              adress:[],
            })
            that.addressList()
          })
        }
      }
    })
  },

  add(){
    wx.navigateTo({
      url: '/pages/mall/adressdetail?type=add'
    })
  },
  edit(e){
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/mall/adressdetail?type=edit&id='+id
    })
  },
  select(e) {
    var item=e.currentTarget.dataset.item;
    if(this.data.prevpage=='order'){
      if(item){
        var pages = getCurrentPages(); // 获取页面栈
        var currPage = pages[pages.length - 1]; // 当前页面
        var prevPage = pages[pages.length - 2]; // 上一个页面
        prevPage.setData({
          defaultAddress:item
        })
      }
      wx.navigateBack({
        delta: 1
      })
    }
  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})